package vietnam.traveltools;

import java.util.ArrayList;

import vietnam.traveltools.module.VoiceAsyncTask;
import android.app.Activity;
import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class SearchActivity extends Fragment {
	private TextView tvNote;
	private LinearLayout lnSpeech;
	private LinearLayout lnSpeechResult;
	private Button btnVoiceSearch;
	private TextView tvSpeech;
	private TextView tvSpeechResult;
	private VoiceAsyncTask vAT;
	protected static final int RESULT_SPEECH = 1;
	private ProgressBar progressBarSearch;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.search_activity, container,
				false);
		progressBarSearch = (ProgressBar) rootView
				.findViewById(R.id.progressBarSearch);
		progressBarSearch.setVisibility(View.INVISIBLE); //

		tvNote = (TextView) rootView.findViewById(R.id.tvNote);
		lnSpeech = (LinearLayout) rootView.findViewById(R.id.lnSpeech);

		lnSpeech.setVisibility(View.INVISIBLE);
		lnSpeechResult = (LinearLayout) rootView
				.findViewById(R.id.lnSpeechResult);

		lnSpeechResult.setVisibility(View.INVISIBLE);

		tvSpeech = (TextView) rootView.findViewById(R.id.tvSpeech);
		tvSpeechResult = (TextView) rootView.findViewById(R.id.tvSpeechResult);

		btnVoiceSearch = (Button) rootView.findViewById(R.id.btnVoiceSearch);
		btnVoiceSearch.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(
						RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

				try {
					startActivityForResult(intent, RESULT_SPEECH);
					doStart();
				} catch (ActivityNotFoundException a) {
					Toast t = Toast
							.makeText(
									getActivity().getApplicationContext(),
									"Ối, có vẻ thiết bị của bạn không hỗ trợ chức năng này",
									Toast.LENGTH_SHORT);
					t.show();
				}

			}
		});

		tvNote.setText("Nhấn button trên để đọc, nếu chưa biết lệnh, hãy xem hướng dẫn");
		tvNote.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent_note = new Intent(getActivity(),
						NoteActivity.class);
				startActivity(intent_note);

			}
		});

		return rootView;
	}

//	public void showPrepare(Intent intent, String casekey) {
//		tvSpeechResult
//				.setText("Bạn yêu cầu tôi mở: "
//						+ casekey
//						+ "\n"
//						+ "Tôi sẽ mở trang tìm kiếm trong 1s tới. Nếu muốn huỷ, chạm bất kỳ trong màn hình để huỷ");
//		try {
//			Thread.sleep(1000);
//			if (touchOff(false)) {
//				getActivity().finish();
//			} else {
//				getActivity().getApplicationContext().startActivity(intent);
//			}
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//
//	}

	public Boolean touchOff(Boolean a) {

		// Set up touch listener for non-text box views to hide keyboard.

		getView().setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				getActivity().finish();
				return false;
			}

		});
		return a = false;

	}

	private void doStart() {
		vAT = new VoiceAsyncTask(getActivity(), progressBarSearch, lnSpeech,
				lnSpeechResult,tvSpeech,tvSpeechResult);
		vAT.execute();
	}

	public static void hideSoftKeyboard(Activity activity) {
		InputMethodManager inputMethodManager = (InputMethodManager) activity
				.getSystemService(Activity.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus()
				.getWindowToken(), 0);
	}

	public void setupUI(View view) {

		// Set up touch listener for non-text box views to hide keyboard.
		if (!(view instanceof EditText)) {

			view.setOnTouchListener(new View.OnTouchListener() {

				public boolean onTouch(View v, MotionEvent event) {
					hideSoftKeyboard(getActivity());
					return false;
				}

			});
		}

		// If a layout container, iterate over children and seed recursion.
		if (view instanceof ViewGroup) {

			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

				View innerView = ((ViewGroup) view).getChildAt(i);

				setupUI(innerView);
			}
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case RESULT_SPEECH: {
			getActivity();
			if (resultCode == Activity.RESULT_OK && null != data) {

				ArrayList<String> text = data
						.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
				lnSpeech.setVisibility(View.VISIBLE);
				tvSpeech.setText(text.get(0));

			}
			break;
		}

		}
	}

}
